package com.java.zhuiyun.api.propmgt.service.impl;

import com.java.zhuiyun.api.propmgt.entity.PropCenter;
import com.java.zhuiyun.api.propmgt.entity.PropMgt;
import com.java.zhuiyun.api.propmgt.mapper.PropMgtMapper;
import com.java.zhuiyun.api.propmgt.model.PropModel;
import com.java.zhuiyun.api.propmgt.service.PropMgtService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @ClassName propMgtServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/9/13 10:09
 **/
@Service
public class PropMgtServiceImpl implements PropMgtService {
    @Autowired
    private PropMgtMapper propMgtMapper;
    /*
    * @Author wuwan
    * @Description 道具分页查询
    * @Date 2023/9/13 10:29
    * @Return
    * @param null
    */
    @Override
    public CommonResult selectProp(PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<PropMgt> propMgts = propMgtMapper.selectProp();
        PageResult pageResult = MybatisPageHelper.getPageResult(propMgts);
        return CommonResult.ok(pageResult);
    }
    /*
    * @Author wuwan
    * @Description 道具添加
    * @Date 2023/9/13 10:41
    * @Return
    * @param null
    */
    @Override
    @Transactional
    public CommonResult addProp(MultipartFile file, PropMgt propMgt) {
        if (file == null){
            return CommonResult.error("请上传图片");
        }
        String s = UploadFileUtils.uploadFile(file);
        propMgt.setPropImg(Integer.parseInt(s));
        propMgt.setCreateTime(new Date());
        propMgt.setUpdateTime(new Date());
        Integer integer = propMgtMapper.addProp(propMgt);
        if (integer >= 1) {
            PropCenter propCenter = new PropCenter();
            List<PropModel> propModels = propMgtMapper.selectUserId();
            for (int i = 0; i < propModels.size(); i++) {
                PropModel propModel = propModels.get(i);
                propCenter.setUserId(propModel.getId());
                propCenter.setPropId(propMgt.getId());
                propCenter.setCreateTime(new Date());
                propCenter.setUpdateTime(new Date());
                propMgtMapper.addPropCenter(propCenter);
            }
            return CommonResult.ok("道具添加成功");
        }
        return CommonResult.error("道具添加失败");
    }
    /*
     * @Author wuwan
     * @Description 道具修改
     * @Date 2023/9/13 10:41
     * @Return
     * @param null
     */
    @Override
    @Transactional
    public CommonResult updateProp(MultipartFile file, PropMgt propMgt) {
        if (file == null){
            propMgt.setUpdateTime(new Date());
            Integer integer = propMgtMapper.updateProps(propMgt);
            if (integer >= 1) {
                return CommonResult.ok("道具修改成功");
            }
            return CommonResult.error("道具修改失败");
        }
        String s = UploadFileUtils.uploadFile(file);
        propMgt.setPropImg(Integer.parseInt(s));
        propMgt.setUpdateTime(new Date());
        Integer integer = propMgtMapper.updateProp(propMgt);
        if (integer >= 1) {
            return CommonResult.ok("道具修改成功");
        }
        return CommonResult.error("道具修改失败");
    }

    @Override
    public CommonResult updateFlag(PropMgt propMgt) {
        Integer integer = propMgtMapper.updateFlag(propMgt);
        if (integer >= 1){
            return CommonResult.ok("启用禁用成功");
        }
        return CommonResult.error("启用禁用失败");
    }

    @Override
    public List<PropModel> userSelectProp(Integer userId) {
        List<PropModel> list=propMgtMapper.userSelectProp(userId);
        return list;
    }

    /**
     * @Author 杨友朋
     * @Description TODO 使用道具
     * @Date 2023/10/16 14:12
     * @Return CommonResult
     * @param userId,propId
     */
    @Override
    public CommonResult userUseProp(Integer userId, Integer propId) {
        if(userId==null || userId==0){
            return CommonResult.error("参数校验失败");
        }
        if(propId==null || propId==0){
            return CommonResult.error("参数校验失败");
        }
        int i = propMgtMapper.usePropCenterByOpenid(userId, propId);
        if(i>0){
            return CommonResult.ok();
        }
        return CommonResult.error("操作失败");
    }

    @Override
    public CommonResult userAddProp(Integer userId, Integer propId) {
        if(userId==null || userId==0){
            return CommonResult.error("参数校验失败");
        }
        if(propId==null || propId==0){
            return CommonResult.error("参数校验失败");
        }
        int i = propMgtMapper.userAddProp(userId, propId);
        if(i>0){
            return CommonResult.ok();
        }
        return CommonResult.error("操作失败");
    }
}
